<?php
/**
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler  
* @package Shuffler_Db 
*/
class Shuffler_Inflector
{
    /**
    * Get the class property name for a class name
    * 
    * used to derive name of property when there is no setter or getter on the model
    * 
    * @return string name of property for class
    */
    public function getPropertyFromClass( $class, $shorteningFactor = 1 )
    {
        $property = explode( '_', $class );
        if( count( $property ) > $shorteningFactor )
        {
            $property = array_slice( $property, count( $property ) - $shorteningFactor, $shorteningFactor );
        }
        foreach( $property as $k => $v )
        {
            $property[ $k ] = strtolower( $v );
        }
        return implode( $property, '_' );
    }
    
    /**
    * Get the 
    * 
    * used to derive name of setters, getters, adders, etc..
    * 
    * @return string command suffix for property
    */
    public function getCommandSuffixFromProperty( $property )
    {
        return ucfirst( $property );
    }
    
    public function quoteIdentifier( $identifier )
    {
        return '`' . $identifier . '`';    
    }
    
    public function quoteValue( $value )
    {
        return '\'' . mysql_real_escape_string( $value ) . '\'';
    }
    
    public function implodeIdentifiers( array $fields )
    {
        $return = '';
        $count = count( $fields );
        $i = 0;
        foreach( $fields as $field )
        {
            $i++;
            $return .= $this->quoteIdentifier( $field );
            if( $i < $count ) $return .= ', ';
        }
        return $return;
    }
    
    public function implodeValues( array $values )
    {
        $return = '';
        $count = count( $values );
        $i = 0;
        foreach( $values as $value )
        {
            $i++;
            $return .= $this->quoteValue( $value );
            if( $i < $count ) $return .= ', ';
        }
        return $return;
    }
    
    /**
    * Inflects a string to replace underscores delimited text with letter case text
    * @param string delimited with underscores
    * @return string in letter case
    */
    public function underscoreToLetterCase( $string )
    {
        $string = ucwords( str_replace( '_', ' ', $string ) );
        return str_replace( ' ', '', $string );
    }
    
    /**
    * Inflects a string to replace letter case with  underscores delimited text
    * @param string in letter case
    * @return string delimited with underscores
    */
    public function letterCaseToUnderscore( $field )
    {
        $field = ucwords( str_replace( '_', ' ', $field ) );
        return str_replace( ' ', '', $field );
    }
}
?>